package com.wlcorp.forcazamis.web.action.storageassign;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.storageassign.IStorageAssignService;
import com.wlcorp.forcazamis.domain.baseinfo.Depot;
import com.wlcorp.forcazamis.domain.storageassign.StorageAssign;
import com.wlcorp.forcazamis.domain.storageassign.StorageAssignSample;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.Validator;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class StorageAssignAction extends DispatchAction {
	
	public ActionForward storageAssignManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("staffList",staffList);
		return mapping.findForward("manager");
	}
	
	public ActionForward getStorageAssignList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageAssign storageAssign = new StorageAssign();
		WebUtil.setRequestToObject(request,storageAssign);
		IStorageAssignService service = ContextUtil.getInstance().getStorageAssignService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getStorageAssignCount(storageAssign).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    storageAssign.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<StorageAssign> storageAssignList = service.getStorageAssignList(storageAssign);
		request.setAttribute("storageAssignList",storageAssignList);
		return mapping.findForward("list");
	}
	
	public ActionForward getStorageAssign(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		StorageAssign storageAssign = new StorageAssign();
		WebUtil.setRequestToObject(request,storageAssign);
		storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(storageAssign.getStorageAssignId());
		if(null == storageAssign){
			storageAssign = new StorageAssign();
		}
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(null);
		request.setAttribute("depotList",depotList);
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("modify");
	}
	
	public ActionForward getStorageAssignDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		StorageAssign storageAssign = new StorageAssign();
		WebUtil.setRequestToObject(request,storageAssign);
		storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(storageAssign.getStorageAssignId());
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward modifyStorageAssign(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageAssign storageAssign = new StorageAssign();
		String storageAssignId = request.getParameter("storageAssignId");
		if(Validator.checkValueIsValid(storageAssignId)){
			storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(Long.valueOf(storageAssignId));
		}else{
			storageAssign.setSubmiter(SessionValue.getInstance(request).getStaff());
			storageAssign.setAssignDate(new Date(Calendar.getInstance().getTimeInMillis()));
			storageAssign.setState(AppConstant.BILL_STATE_SUBMITED);
		}
		WebUtil.setRequestToObject(request,storageAssign);
		ContextUtil.getInstance().getStorageAssignService().saveStorageAssign(storageAssign);
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(null);
		request.setAttribute("depotList",depotList);
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyStorageAssignDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String[] subSampleIdArray = request.getParameterValues("subSampleId");
		String[] inCountArray = request.getParameterValues("assignCount"); 
		String[] priceArray = request.getParameterValues("price"); 
		String[] totalPriceArray = request.getParameterValues("totalPrice"); 
		String[] memoArray = request.getParameterValues("memo");
		String storageAssignId = request.getParameter("storageAssignId");
		List<StorageAssignSample> storageAssignSampleList = new ArrayList<StorageAssignSample>();
		if(null != subSampleIdArray){
			for (int i = 0; i < subSampleIdArray.length; i++) {
				StorageAssignSample storageAssignSample = new StorageAssignSample();
				storageAssignSample.setSubSampleId(Long.valueOf(subSampleIdArray[i]));
				storageAssignSample.setAssignCount(Long.valueOf(inCountArray[i]));
				storageAssignSample.setPrice(Double.valueOf(priceArray[i]));
				storageAssignSample.setTotalPrice(Double.valueOf(totalPriceArray[i]));
				storageAssignSample.setMemo(memoArray[i]);
				storageAssignSample.setStorageAssignId(Long.valueOf(storageAssignId));
				storageAssignSampleList.add(storageAssignSample);
			}
		}
		ContextUtil.getInstance().getStorageAssignService().saveStorageAssignSample(storageAssignSampleList);
		StorageAssign storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(Long.valueOf(storageAssignId));
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward printStorageAssign(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StorageAssign storageAssign = new StorageAssign();
		WebUtil.setRequestToObject(request,storageAssign);
		storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(storageAssign.getStorageAssignId());
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("print");
	}
	
	public ActionForward withDrawStorageAssign(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String storageAssignId = request.getParameter("storageAssignId");
		StorageAssign storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(Long.valueOf(storageAssignId));
		ContextUtil.getInstance().getStorageAssignService().withDrawStorageAssign(storageAssign);
		request.setAttribute("storageAssign",storageAssign);
		return mapping.findForward("modifyDetail");
	}
	
	public ActionForward deleteStorageAssign(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Long storageAssignId = Long.valueOf(request.getParameter("storageAssignId"));
		StorageAssign storageAssign = ContextUtil.getInstance().getStorageAssignService().getStorageAssignById(storageAssignId);
		ContextUtil.getInstance().getStorageAssignService().deleteStorageAssign(storageAssign);
		return getStorageAssignList(mapping,form,request,response);
	}
}
